package mylab.starters.secure.autoconfigure;

import lombok.RequiredArgsConstructor;
import mylab.starters.secure.interceptor.SecureInterceptor;
import mylab.starters.secure.model.Rule;
import mylab.starters.secure.support.SecureExpressionHandler;
import mylab.starters.secure.support.SecureUserArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * <p>
 * 自动装配类
 * </p>
 */
@Configuration
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@ConditionalOnProperty(value = "magic.secure.enabled", havingValue = "true", matchIfMissing = true)
public class SecureAutoConfiguration implements WebMvcConfigurer {
    private final SecureUserArgumentResolver secureUserArgumentResolver;
    private final SecureExpressionHandler secureExpressionHandler;
    private final List<Rule> ruleList;
    private final List<String> whiteList;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(secureUserArgumentResolver);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SecureInterceptor(ruleList, secureExpressionHandler))
                .excludePathPatterns(whiteList);
    }
}
